//
//  CellTopView.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/5/16.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit

class CellTopView: UIView {

    var  status: Status?{
        didSet{
            let user = status?.user
            //设置头像
            iconIMg.sd_setImageWithURL(user?.userIcon)
            // set title
            nameLabel.text = user?.name
            // 设置认证图片
            verifiedView.image = user?.verifiedIMg
            // 更新约束
            let size = nameLabel.sizeThatFits(CGSize(width: CGFloat(MAXFLOAT), height: 15))
            nameLabel.snp_updateConstraints { (make) in
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            
            vipView.image = user!.mbrankImage
            // 更新约束  // 设置表表时间
            timeLab.text = status?.sina_time
            let size1 = timeLab.sizeThatFits(CGSize(width: CGFloat(MAXFLOAT), height: 13))
            timeLab.snp_updateConstraints { (make) in
                make.width.equalTo(size1.width)
                make.height.equalTo(size1.height)
            }
            
            sourceLabel.text = status?.source
            
                       
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    
    private func setUpUI() {
        addSubview(iconIMg)
        addSubview(verifiedView)
        addSubview(nameLabel)
        addSubview(vipView)
        addSubview(timeLab)
        addSubview(sourceLabel)
        
        let width =  50.0 ; let padding = 10
        iconIMg.snp_makeConstraints { (make) in
            make.width.height.equalTo(width)
            make.left.top.equalTo(padding)
        }
        
        verifiedView.snp_makeConstraints { (make) in
            make.width.height.equalTo(14)
            make.center.equalTo(iconIMg).offset(CGPoint(x: width * 0.35, y: width*0.35))
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconIMg.snp_right).offset(10)
            make.top.equalTo(iconIMg)
            make.width.equalTo(2)
            make.height.equalTo(15)
        }
        
        vipView.snp_makeConstraints { (make) in
            make.width.height.equalTo(14)
            make.top.equalTo(iconIMg)
            make.left.equalTo(nameLabel.snp_right).offset(8)
        }
        
        timeLab.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp_bottom).offset(8)
            make.width.equalTo(2)
            make.height.equalTo(13)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLab.snp_right).offset(8)
            make.top.equalTo(timeLab)
            make.width.equalTo(200)
            make.height.equalTo(13)
        }
        
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 懒加载
    /**
     *  头像
     */
    private lazy var iconIMg: UIImageView = {
        let img = UIImageView(image: UIImage(named: "avatar_default_big"))
        img.layer.cornerRadius = 25
        img.layer.masksToBounds = true
        return img
    }()
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    
    /// 昵称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
    
    
    /**
     *  会员标志
     */
    private lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    // 时间
    private lazy var timeLab: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.darkGrayColor()
        lab.font = UIFont.systemFontOfSize(12)
        return lab
    }()
    
    
    /// 来源
    private lazy var sourceLabel: UILabel =
        {
            let label = UILabel()
            label.textColor = UIColor.darkGrayColor()
            label.font = UIFont.systemFontOfSize(12)
            return label
    }()
}
